<?php
//$Id: UploadController.php 644 2013-01-20 10:21:08Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

Class UploadController extends MemberController
{

    public function thumbAction() {
        $config = Base::getConfig('common');
        $option = array(
            'upload_dir'  => $config['upload_path'],
            'allow_type'  => 'image',
            'check_image' => true,
            'max_length'  => 512*1024,
            'image_min_width'  => 200,
            'image_min_height' => 200,
        );

        $up = new Upload('thumbdata', $option);
        $error = $up->uploadFile();
        if($error != UPLOAD_ERR_OK){
            $arr['status'] = false;
            switch($error){
                case UPLOAD_ERR_FILE_SIZE   : $arr['err'] = '上传图片必须小于 512K'; break;
                case UPLOAD_ERR_FILE_TYPE   : $arr['err'] = '上传类型只能为 JPG, PNG 或者 GIF'; break;
                case UPLOAD_ERR_IMAGE_SMALL : $arr['err'] = '上传图片尺寸必须大于 200x200 像素'; break;
                default : $arr['err'] = '文件上传错误('.$error.')';
            }
            $arr['msg'] = '';
        }else{
            $arr['status'] = true;
            $arr['err'] = '';
            $arr['msg'] = array('url'=>$up->file['file'], 'localname' => $up->file['name'], 'id' => 1);
        }
        $this->renderJSON($arr);
    }


    public function imageAction() {
        $config = Base::getConfig('common');
        $option = array(
            'upload_dir'  => $config['upload_path'],
            'allow_type'  => 'image',
            'check_image' => true,
            'max_length'  => 2048*1024,
            'image_min_width'  => 200,
            'image_min_height' => 200,
        );

        $up = new Upload('imagedata', $option);
        $error = $up->uploadFile();
        if($error != UPLOAD_ERR_OK){
            $arr['status'] = false;
            switch($error){
                case UPLOAD_ERR_FILE_SIZE   : $arr['err'] = '上传图片必须小于 2.0M'; break;
                case UPLOAD_ERR_FILE_TYPE   : $arr['err'] = '上传类型只能为 JPG, PNG 或者 GIF'; break;
                case UPLOAD_ERR_IMAGE_SMALL : $arr['err'] = '上传图片尺寸必须大于 200x200 像素'; break;
                default : $arr['err'] = '文件上传错误('.$error.')';
            }
            $arr['msg'] = '';
        }else{
            $arr['status'] = true;
            $arr['err'] = '';
            $arr['msg'] = array('url'=>$up->file['file'], 'localname' => $up->file['name'], 'id' => 1);
        }
        $this->renderJSON($arr);
    }

    public function fileAction() {
        $config = Base::getConfig('common');
        $option = array(
            'upload_dir'  => $config['upload_path'],
            'allow_type'  => 'image|zip|rar|txt|doc|docx|csv|xls|xlsx',
            'check_image' => false,
            'max_length'  => 2048*1024,
        );

        $up = new Upload('filedata', $option);
        $error = $up->uploadFile();
        if($error != UPLOAD_ERR_OK){
            $arr['status'] = false;
            switch($error){
                case UPLOAD_ERR_FILE_SIZE   : $arr['err'] = '上传图片必须小于 2.0M'; break;
                case UPLOAD_ERR_FILE_TYPE   : $arr['err'] = '上传类型只能为图片，文档或压缩包'; break;
                case UPLOAD_ERR_IMAGE_SMALL : $arr['err'] = '上传图片尺寸必须大于 200x200 像素'; break;
                default : $arr['err'] = '文件上传错误('.$error.')';
            }
            $arr['msg'] = '';
        }else{
            $arr['status'] = true;
            $arr['err'] = '';
            $arr['msg'] = array('url'=>$up->file['file'], 'localname' => $up->file['name'], 'id' => 1);
        }
        $this->renderJSON($arr);
    }
}


